﻿using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Models
{
    public class MinisterTimeBlockPresentationModel : INotifyPropertyChanged
    {
        public event EventHandler<DataEventArgs<MinisterTimeBlockPresentationModel>> Saved;
        public event EventHandler<DataEventArgs<MinisterTimeBlockPresentationModel>> Cancelled;

        public DelegateCommand<object> SaveCommand { get; private set; }
        public DelegateCommand<object> CancelCommand { get; private set; }

        #region Properties

        public DateTime Date { get; set; }

        private ObservableCollection<Minister> ministers;
        public ObservableCollection<Minister> Ministers
        {
            get
            {
                return ministers;
            }
            set
            {
                if (!value.Equals(ministers))
                {
                    ministers = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Ministers"));
                }
            }
        }

        private ObservableCollection<MinisterTimeBlock> ministerTimeBlocks;
        public ObservableCollection<MinisterTimeBlock> MinisterTimeBlocks
        {
            get
            {
                return ministerTimeBlocks;
            }
            set
            {
                if (!value.Equals(ministerTimeBlocks))
                {
                    ministerTimeBlocks = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("MinisterTimeBlocks"));
                }
            }
        }

        #endregion

        #region Contructor
        public MinisterTimeBlockPresentationModel()
        {
            SaveCommand = new DelegateCommand<object>(Save);
            CancelCommand = new DelegateCommand<object>(Cancel);

            PropertyChanged += OnPropertyChangedEvent;
        }
        #endregion

        private void OnPropertyChangedEvent(object sender, PropertyChangedEventArgs e)
        {
            SaveCommand.RaiseCanExecuteChanged();
        }

        private void Save(object obj)
        {
            OnSaved(new DataEventArgs<MinisterTimeBlockPresentationModel>(this));
        }

        private void Cancel(object obj)
        {
            OnCancelled(new DataEventArgs<MinisterTimeBlockPresentationModel>(this));
        }

        private void OnSaved(DataEventArgs<MinisterTimeBlockPresentationModel> e)
        {
            var savedHandler = Saved;

            if (savedHandler != null)
                savedHandler(this, e);
        }

        private void OnCancelled(DataEventArgs<MinisterTimeBlockPresentationModel> e)
        {
            var cancelHandler = Cancelled;

            if (cancelHandler != null)
                cancelHandler(this, e);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
